<?php

namespace Admin\Controller;

use Think\Controller;

class MyUserController extends CommonController
{
    function __construct()
    {
        parent::__construct();
    }

    //未借款用户转化列表
    public function my_user()
    {
        $this->display();
    }

    //客服经理优质用户列表
    public function manager_user()
    {
        $this->display();
    }

    //优质用户认证详情
    public function user_info()
    {
        $this->display();
        die;
        $vid = I('vid');
        $mobile = I('mobile');
        //用户识别
        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $vid])->field('photo,photo1,photo2,front,behind')->find();
        //京东
        $field = 'name,area,address,mobile,default';
        $jd_add = M('jdtd_receiver_list', '', 'ZX_R')->where(['vid' => $vid])->field($field)->select();

        //淘宝
        $tb_add = M('td_tb_receiver_list', '', 'ZX_R')->where(['vid' => $vid])->field('area,address,name,mobile')->select();

        //支付宝 取最近的10
        $zfb_record = M('mh_alipay_trans_info', '', 'ZX_R')->where(['vid' => $vid])->field('trade_type,UNIX_TIMESTAMP(trade_time) as tan_time,trade_name,trade_partner,trade_amount')->order('tan_time desc')->limit(10)->select();

        //话费
        $mobile_count = M('mobile_basicinfo', '', 'ZX_R')->where(['vid' => $vid])->field('mobile,vip_level,score,cert_addr,amount,user_name')->select();

        //用户通话记录
        $field = 'call_cost,call_land_type,call_type_name,call_roam_cost,call_other_number,call_start_time,call_time';
        $callrecord = M('mobile_callrecord', '', 'ZX_R')->where(['mymobile' => $mobile])->field($field)->select();

        //是否处于严重负债或者逾期状态
        //jjd
        $jjd_info = M('scrapy_jjd', '', 'FK_R')->where(['mobile' => $mobile])->field('mobile,n_reported_cnt,n_current_borrow_amt,n_current_overdue_amt,n_current_overdue_cnt,n_borrow_amt,n_borrow_cnt')->select();
        //jdb
        $jdb_info = M('scrapy_jdb', '', 'FK_R')->where(['mobile' => $mobile])->field('mobile,message,sfz,name,ctime')->select();

        //关联用户是否存在严重逾期
        $relative_user = M('user_mobilebook', '', 'KF_USER_TX')->where(['vid' => $vid])->getField('mobile', true);
        $where = [];
        $where['mobile'] = array('in', $relative_user);
        $user = M('user', '', 'USER')->where($where)->getField('vid', true);
        $field = 'a.repayment_no,b.mobile,b.truename,a.status,a.overdue_day';
        $relative_overdue = M('user_overdue', '', 'URGE')->join('a join dxd_user.user b on a.vid=b.vid')->where(['a.vid' => array('in', $user)])->field($field)->select();

        //月消费情况是否存在异常
        $month_coast = M('mobile_bill', '', 'ZX_R')->where(['vid' => $vid])->field('bill_cycle,bill_fee,paid_amount,unpaid_amount,breach_amount,bill_total')->select();

        //通讯录高频联系人是否存在作假 前10个
        $user_tx = M('user_mobilebook', '', 'KF_USER_TX')->query('SELECT a.name , a.mobile as call_other_number ,IFNULL(b.num,0) as num
                                                              from user_mobilebook a 
                                                              left join (SELECT call_other_number,COUNT(call_other_number) as num 
                                                                          FROM mobile_callrecord_s 
                                                                          WHERE mymobile=\'' . $mobile . '\' 
                                                                          GROUP BY call_other_number) b 
                                                                          on a.mobile=b.call_other_number 
                                                                          WHERE  a.vid=\'' . $vid . '\'
                                                                          ORDER BY num desc LIMIT ' . 0 . ',' . 10);
        $this->display();
    }

    // 客服负责用户列表  用户借款转化
    public function my_user_list()
    {
        $type = I('type', 1);//1 未借款，2 已借款
        $n = I('page', 1);
        $num = 10;
        $page = ($n - 1) * $num;
        $where = [];
        if ($_SESSION['role_id'] == 2) {
            $where['a.admin_id'] = $_SESSION['id'];
        }
        $field = "c.cert_time,c.id as user_id,c.mobile,c.truename as name,c.ctime,c.sex,a.vid,a.admin_id,e.name as admin_name,c.type";
        $where['a.type'] = 1;//未借款转化类
        if ($type == '1'){
//            $time_now=strtotime(date('Y-m-d',time()));
//            $s_time=$time_now-3*24*3600;
//            $where['d.mobilefirm'] = array('notlike', 'iPhone%');
            $where['b.vid'] = array('exp', 'is null');
//            if($_SESSION['id']=='957'){//运营的就显示三天之内的
//                $where['c.cert_time']=array('between',array($s_time,$time_now));
//                $count = M('kefu_ownuser', '', 'KEFU_R')->join('a left join dxd_caiwu.user_borrow b on a.vid=b.vid')->join('join dxd_user.user_login d on d.vid=a.vid')->join('join dxd_user.user c on a.vid=c.vid and c.cert_status=2 and c.type!=2 and c.status=1')->where($where)->count();
//                $data = M('kefu_ownuser', '', 'KEFU_R')->join('a left join dxd_caiwu.user_borrow b on a.vid=b.vid')->join('join dxd_user.user c on a.vid=c.vid and c.cert_status=2 and c.type!=2 and c.status=1')->join('join dxd_user.user_login d on d.vid=a.vid')->join('join dxd_kefu.admin e on e.id=a.admin_id')->order('a.id desc')->where($where)->field($field)->limit($page, $num)->select();
//            } else{
                $count = M('kefu_ownuser', '', 'KEFU_R')->join('a left join dxd_caiwu.user_borrow b on a.vid=b.vid')->join('join dxd_user.user_login d on d.vid=a.vid')->join('join dxd_user.user c on a.vid=c.vid and c.cert_status=2 and c.type!=2 and c.status=1')->where($where)->count();
                $data = M('kefu_ownuser', '', 'KEFU_R')->join('a left join dxd_caiwu.user_borrow b on a.vid=b.vid')->join('join dxd_user.user c on a.vid=c.vid and c.cert_status=2 and c.type!=2 and c.status=1')->join('join dxd_user.user_login d on d.vid=a.vid')->join('join dxd_kefu.admin e on e.id=a.admin_id')->where($where)->field($field)->limit($page, $num)->order('a.id desc')->select();
//            }

        } else if ($type == '2'){
            $sub = 'SELECT `ctime`,`is_success`,`vid` FROM dxd_caiwu.`user_borrow` GROUP BY vid';
            $where['b.ctime'] = array('egt', strtotime(date('Y-m-01', time())));//本月借款转化
            $where['b.is_success'] = 2;
            $where['d.state'] = 1;//接听
            $where['d.calltype'] = 1;//外呼
            if ($_SESSION['role_id'] == 2) {
                $where['d.admin'] = $_SESSION['name'];//外呼
            }
            $count = M('kefu_ownuser', '', 'KEFU')->table('(' . $sub . ') b')->join('join dxd_kefu.kefu_ownuser a on a.vid=b.vid and a.type=1')->join('join dxd_kefu.call_info d on d.vid=a.vid')->where($where)->count('DISTINCT a.vid');
            $data = M('kefu_ownuser', '', 'KEFU')->table('(' . $sub . ') b')->join('join dxd_kefu.kefu_ownuser a on a.vid=b.vid and a.type=1')->join('join dxd_user.user c on a.vid=c.vid')->join('join dxd_kefu.call_info d on d.vid=a.vid')->join('join dxd_kefu.admin e on e.id=a.admin_id')->where($where)->order('c.cert_time desc')->field($field)->group('a.vid')->limit($page, $num)->select();
        }

         $t_time=time()-4*86400;
        foreach ($data as $k => $v) {
            //最近备注
            $w = [];
            $w['vid'] = $v['vid'];
            $w['calltype'] =1;
            $w['state'] =1;
            $w['ctime'] =['egt',$t_time];

//            $w['admin'] = $v['admin_name'];
            $mark = M('call_info', '', 'KEFU_R')->where($w)->order('id desc')->limit(1)->getfield('content');
            if (!empty($mark)) {
                $data[$k]['mark'] = $mark;
            } else {
                $data[$k]['mark'] = "---";
            }
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $data[$k]['cert_time'] = date('Y-m-d H:i:s', $v['cert_time']);
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            if ($v['sex'] == '1') {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == '2') {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }

            //最近打电话时间

            $time=M('call_info','','KEFU_R')->where($w)->order('id desc')->limit(1)->field('ctime')->find();
            if(!empty($time)){
                $data[$k]['call_time']=date('Y-m-d H:i:s',$time['ctime']);
            }else{
                $data[$k]['call_time']='--';
            }
            if($v['type']==1){
                $data[$k]['type']='共享现金';
            }else if($v['type']==3){
                $data[$k]['type']='马上游呗';
            }


        }

        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'total' => $tol, 'count' => $count, 'result' => $data));

    }

    //用户池子
    public function good_user_all()
    {
        $type = I('type', 4);
        $n = I('page', 1);
        $num = 10;
        $page = ($n - 1) * $num;
        $where = [];
        $search = I('search', '');
        if ($search != '') {
            $where['c.mobile|c.id|c.truename'] = array('like', '%' . $search . '%');
//            $w['c.mobile|c.id|c.truename']=array('like','%'.$search.'%');
        }

        //优质用户 ：借款三次以上，逾期小于三天
        $where['b.vid'] = array('exp', 'is null');
        $where['a.borrow_num'] = array('gt', 3);
        $where['a.repay_num'] = array('gt', 3);
        $where['a.overdue_num'] = array('elt', 0);
        $count = M('user_borrow_statistics', '', 'USER_R')->join('a left join dxd_kefu.kefu_ownuser b on a.vid=b.vid and b.type=2 ')->join(' join dxd_user.user c on a.vid=c.vid')->where($where)->count();
        $field = 'c.vid,c.truename as name ,c.ctime,c.mobile,c.sex';
        $data = M('user_borrow_statistics', '', 'USER_R')->join('a left join dxd_kefu.kefu_ownuser b on a.vid=b.vid and b.type=2 ')->join(' join dxd_user.user c on a.vid=c.vid')->where($where)->LIMIT($page, $num)->order('c.ctime desc')->field($field)->select();
        foreach ($data as $k => $v) {
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            $data[$k]['ctime'] = date('Y-m-d H:i', $v['ctime']);
            if ($v['sex'] == 1) {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == 2) {
                $data[$k]['sex'] = '女';
            } else if ($v['sex'] == 0) {
                $data[$k]['sex'] = '未知';
            }
            //用户收益
            $borrow_money = M('user_repayment', '', 'CAIWU')->where(['vid' => $v['vid'], 'status' => 3])->getfield('repay_money', true);
            $data[$k]['profit'] = intval(array_sum($borrow_money) * (16 / 100));
            $data[$k]['type'] = $type;
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'total' => $tol, 'count' => $count, 'result' => $data));

    }

    //客服经理获取用户
    public function get_user()
    {
        $vid = I('vid');
        $has = M('kefu_ownuser', '', 'KEFU')->where(['vid' => $vid, 'type' => 2])->find();
        if (!empty($has)) {
            $update = M('kefu_ownuser', '', 'KEFU')->where(['vid' => $vid])->setField('admin_id', $_SESSION['id']);
        } else {
            $add = array(
                'vid' => $vid,
                'ctime' => time(),
                'admin_id' => $_SESSION['id'],
                'type' => 2
            );
            $update = M('kefu_ownuser', '', 'KEFU')->add($add);
        }
        if ($update) {
            $this->ajaxReturn(['code' => '1001', 'result' => '操作成功']);
            die;
        }
        $this->ajaxReturn(['code' => '1002', 'result' => '操作失败']);

    }

    //客服经理 优质用户 粘性作用
    public function good_user_list()
    {
        $type = I('type', 1);
        $n = I('page', 1);
        $num = 10;
        $page = ($n - 1) * $num;
        $where = [];
        $search = I('search', '');
        if ($search != '') {
            $where['c.mobile|c.id|c.truename'] = array('like', '%' . $search . '%');
            $w['c.mobile|c.id|c.truename'] = array('like', '%' . $search . '%');
        }
        if ($_SESSION['role_id'] == 2) {
            $where['a.admin_id'] = $_SESSION['id'];
            $w['a.admin_id'] = $_SESSION['id'];
        } else {
            $w['a.admin_id'] = array('neq', 0);
        }
        if ($type == 1) {
            $where['a.contact_status'] = array('neq', 1);
            $w['a.contact_status'] = array('neq', 1);
        } else if ($type == 2) {
            $where['a.contact_status'] = array('eq', 1);
            $w['a.contact_status'] = array('eq', 1);
        } else if ($type == 4) {

        }
        $field = "c.id as user_id,c.mobile,c.truename as name,c.ctime,c.sex,a.vid,b.name as admin_name,a.vid";
        $where['a.type'] = 2;

        $where['c.cert_status'] = array('eq', 2);
        if ($type != 2) {
            $where['c.waitdays'] = array('lt', time());
            $where['d.repay_num'] = array('gt', 3);
            $where['d.overdue_num'] = array('elt', 0);
            $w['d.repay_num'] = array('gt', 3);
            $w['d.overdue_num'] = array('elt', 0);
        }
        $w['c.waitdays'] = array('lt', time());
        $w['c.cert_status'] = array('eq', 2);
        $w['a.type'] = 2;
        $count = M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_user.user c on a.vid=c.vid ')->join('join dxd_user.user_borrow_statistics d on a.vid=d.vid')->where($w)->count();
        $data = M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_user.user c on a.vid=c.vid ')->join('join dxd_user.user_borrow_statistics d on a.vid=d.vid')->join(' join dxd_kefu.admin b on a.admin_id=b.id')->order('a.upd_time desc')->where($where)->field($field)->limit($page, $num)->select();

        foreach ($data as $k => $v) {
            $data[$k]['ctime'] = date('Y-m-d H:i:s', $v['ctime']);
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
            if ($v['sex'] == '1') {
                $data[$k]['sex'] = '男';
            } else if ($v['sex'] == '2') {
                $data[$k]['sex'] = '女';
            } else {
                $data[$k]['sex'] = '未知';
            }
            $data[$k]['type'] = $type;
            //用户收益
            $borrow_money = M('user_repayment', '', 'CAIWU')->where(['vid' => $v['vid'], 'status' => 3])->getfield('repay_money', true);
            $data[$k]['profit'] = intval(array_sum($borrow_money) * (16 / 100));
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'total' => $tol, 'count' => $count, 'result' => $data));

    }

    //取消或转到微信用户
    public function turn_wx_user()
    {
        $type = I('type');//1转到微信，2取消转微信
        $vid = I('vid');
        switch ($type) {
            case 1:
                $arr = array('contact_status' => 1, 'upd_time' => time());
                $update = M('kefu_ownuser', '', 'KEFU')->where(['type' => 2, 'vid' => $vid])->save($arr);
                add_log("已加用户微信" . $vid);
                break;

            case 2:
                $arr = array('contact_status' => 0, 'upd_time' => time());
                $update = M('kefu_ownuser', '', 'KEFU')->where(['type' => 2, 'vid' => $vid])->save($arr);
                add_log("将用户从微信移除" . $vid);
                break;
        }
        if ($update) {
            $this->ajaxReturn(['code' => '1001', 'result' => '操作成功']);
            die;
        }
        $this->ajaxReturn(['code' => '1002', 'result' => '操作失败']);
    }

    //客服经理已添加微信用户-借款中用户
    public function in_borrow()
    {
        $n = I('page', 1);
        $num = 10;
        $page = ($n - 1) * $num;
        if ($_SESSION['role_id'] == 2) {
            $where['a.admin_id'] = $_SESSION['id'];
        }
        $search = I('search', '');
        if ($search != '') {
            $where['d.mobile|d.id|d.truename'] = array('like', '%' . $search . '%');
        }
        $where['a.contact_status'] = 1;
        $where['a.type'] = 2;
        $where['b.is_success'] = 2;
        $where['b.status'] = 1;
        $field = 'd.mobile,d.truename as name,b.status as borrow_status,c.is_overdue,c.repay_time,e.name as admin,c.repayment_no';
        $count = M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_caiwu.user_borrow b on b.vid=a.vid')->join('join dxd_user.user d on d.vid=a.vid')->where($where)->count();
        $data = M('kefu_ownuser', '', 'KEFU_R')->join('a join dxd_caiwu.user_borrow b on b.vid=a.vid')->join('join dxd_caiwu.user_repayment c on c.borrow_no=b.borrow_no')->join('join dxd_user.user d on d.vid=a.vid')->join('join dxd_kefu.admin e on e.id=a.admin_id')->order(' c.is_overdue desc,c.repay_time asc')->field($field)->where($where)->limit($page, $num)->select();
        foreach ($data as $k => $v) {

            $reapy_status = M('user_overdue', '', 'URGE')->where(['repayment_no' => $v['repayment_no']])->getfield('status');
            if ($reapy_status == 1) {
                $data[$k]['repay_status'] = '未还款';
            } else if ($reapy_status == 2) {
                $data[$k]['repay_status'] = '已还款';
            } else if ($reapy_status == 3) {
                $data[$k]['repay_status'] = '已续期';
            } else if ($reapy_status == 4) {
                $data[$k]['repay_status'] = '已分期';
            } else {
                $data[$k]['repay_status'] = '未还款';
            }
            $data[$k]['repay_time'] = date('Y-m-d H:i:s', $v['repay_time']);
//            if($v['borrow_status']==1){
//                $data[$k]['borrow_status']='未还款';
//            }else if ($v['borrow_status']==2){
//                $data[$k]['borrow_status']='已还款';
//            }else if ($v['borrow_status']==3){
//                $data[$k]['borrow_status']='已取消借款';
//            }
            if ($v['is_overdue'] == 1) {
                $data[$k]['is_overdue'] = '正常';
            } else if ($v['is_overdue'] == 2) {
                $data[$k]['is_overdue'] = '逾期';
            }
            $data[$k]['user_mobile'] = get_mobile($v['mobile']);
        }
        $count = intval($count);
        $tol = ceil($count / $num);
        $this->ajaxReturn(array('code' => '1001', 'result' => 'success', 'total' => $tol, 'count' => $count, 'result' => $data));
    }

    //无法添加微信的用户 则转回给其他客服经理
    public function back_to_user()
    {
        $vid = I('vid');
        $delete = M('kefu_ownuser', '', 'KEFU')->where(['type' => 2, 'vid' => $vid])->delete();

        if ($delete) {
            add_log('踢出用户' . $vid);
            $this->ajaxReturn(['code' => '1001', 'result' => '操作成功']);
            die;
        }
        $this->ajaxReturn(['code' => '1002', 'result' => '操作失败']);
        die;
    }

    //优质用户详情==废
    public function user_info_table()
    {
        $vid = I('vid');
        $mobile = I('mobile');
        //用户识别
        $user_pic = M('user_idcard_img', '', 'USER')->where(['vid' => $vid])->field('photo,photo1,photo2,front,behind')->find();
        //京东
        $field = 'name,area,address,mobile,default';
        $jd_add = M('jdtd_receiver_list', '', 'ZX_R')->where(['vid' => $vid])->field($field)->select();
        //淘宝
        $tb_add = M('td_tb_receiver_list', '', 'ZX_R')->where(['vid' => $vid])->field('area,address,name,mobile')->select();

        $this->assign(array('user_pic' => $user_pic, 'jd'));
        $this->display();
    }


    /***
     *
     */



}

